#! python
# -*- coding: utf-8 -*-
import random
from math import copysign

from objects_on_map import Pixel
from noise import get_around_pixels


def generate_health_points_on_map(altitude_noise):
    y = 0
    pixel = Pixel()
    rock_types = range(3, 9)
    rock_blocks_for_health_point = (3, 4)
    around_step = 5
    health_point_step = 50

    health_points_may_list = []

    for y_height in altitude_noise:
        x = 0

        for x_pixel in y_height:
            # Not Dirt
            if x_pixel in rock_types:

                if random.randint(0, 10) == 0:
                    luck = 0
                    pixel.x = x
                    pixel.y = y
                    around_pixels = get_around_pixels(pixel, around_step)
                    #print len(around_pixels)
                    #altitude_noise[y][x] = 31

                    for one_pixel in around_pixels:
                        value_in_altitude_noise = altitude_noise[one_pixel[1]][one_pixel[0]]

                        if value_in_altitude_noise in rock_blocks_for_health_point:
                            luck += 1

                    if luck > 110:
                        make_point = True

                        for point in health_points_may_list:
                            point_x = point[0]
                            point_y = point[1]

                            delta_x = copysign(x - point_x, 0)
                            delta_y = copysign(y - point_y, 0)

                            if delta_x + delta_y < health_point_step:
                                make_point = False

                        if make_point:
                            luck = 0
                            around_pixels = get_around_pixels(pixel, around_step)

                            for one_pixel in around_pixels:
                                value_in_altitude_noise = altitude_noise[one_pixel[1]][one_pixel[0]]

                                if value_in_altitude_noise > 9:
                                    luck += 1

                            if luck == 0:
                                altitude_noise[y][x] = 31

                        health_points_may_list.append([x, y])

            x += 1
        y += 1
    return altitude_noise
